package com.usian.controller;

import com.usian.feign.UserFeign;
import com.usian.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class UserController {
    //从eureka注册中心获取服务端的ip、端口、要调用的服务
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    //访问Rest服务的客户端
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserFeign userFeign;

    @RequestMapping("/consumer/findAll")
    public List<Users> getUser(){
        return userFeign.findAll();
    }

    @RequestMapping("findByname/{nam}")
    public String findByname(@PathVariable String nam){
        return userFeign.findByname(nam);
    }

    @RequestMapping("findBynamea")
    public String findBynamea(@RequestParam String nam){
        return userFeign.findByname(nam);
    }

    @RequestMapping("findById")
    public String findById(@RequestParam Integer id){
        return userFeign.findById(id);
    }

    @RequestMapping("findBynameandid/{nam}/{id}")
    public String findBynameandid(@PathVariable String nam,@PathVariable Integer id){
        return userFeign.findBynameandid(nam,id);
    }


/*    @RequestMapping(value="consumer/findAll",method= RequestMethod.GET)
    public User getUserBy(@PathVariable Integer id){
        ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        //指定要调用的服务
        String url = "http://"+si.getHost()+":"+si.getPort()+"/findALL";
        return restTemplate.getForObject(url, User.class);
    }*/
    @RequestMapping(value="/consumer/findAll",method= RequestMethod.GET)
    public List<Users> getUserBy(){
        ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        //指定要调用的服务
        String url = "http://"+si.getHost()+":"+si.getPort()+"/findAll";
        return restTemplate.getForObject(url, List.class);
    }
}
